package cn.stimd.spring.beans;

import org.springframework.util.ClassUtils;

import java.beans.PropertyChangeEvent;

/**
 * Created by Stimd
 * 项目地址：https://gitee.com/stimd/spring-wheel
 * 公众号：Java编程探微
 */
public class TypeMismatchException extends PropertyAccessException{
    private transient Object value;
    private Class<?> requiredType;


    public TypeMismatchException(PropertyChangeEvent propertyChangeEvent, Class<?> requiredType, Throwable cause) {
        super(propertyChangeEvent, "Failed to convert value of type '" + ClassUtils.getDescriptiveType(propertyChangeEvent.getNewValue())
                + "' to required type '" + requiredType.getName() + "'", cause);
        this.requiredType = requiredType;
    }


    public TypeMismatchException(Object value, Class<?> requiredType, Throwable cause) {
        super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
                (requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""), cause);
        this.value = value;
        this.requiredType = requiredType;
    }

    public Object getValue() {
        return value;
    }

    public Class<?> getRequiredType() {
        return requiredType;
    }
}
